<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
		array('op'=>'list','title'=>'标签规则'),
		array('op'=>'edit','title'=>'添加标签规则'),
);
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['workerids'])){
		$params['workerids']=intval($_RQ['workerids']);
		$return['params']['workerids']=$_RQ['workerids'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_member_settag_key',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=array('username'=>$item['username'],'avatar'=>$item['avatar'],'nickname'=>$item['nickname']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：基于开启会话存档功能，当客户与员工聊天中，客户命中关键词时，该员工可自动打上客户标签，实现对客户标签的自动化管理，客户画像更智能精准。</br>
		1、标签规则删除后，其关联的客户标签，在生效员工列表，该标签不再显示。。</br>
		2、标签规则设置的关键词，当其被修改或是删除，在生效员工列表，查看详情时，依然可看到之前所触发关键词的客户打标签的记录。</br>
		','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'keys','title'=>'标签规则','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'title'=>'选择标签','name'=>'tagid','action'=>'jy_scrm/member.get_tag/'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
		),	
	);
	$return['types']=array('否','是');
	$return['status']=array('未开启','已开启');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_member_settag_key',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['item']['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
					array('type'=>'choose','title'=>'选择标签','name'=>'tagid','action'=>'jy_scrm/member.get_tag/'),
					array('type'=>'text','title'=>'聊天自动打标签?','name'=>'keys','help'=>'聊天中包含以下关键词则自动打上标签。同时添加多个关键词，请用“|隔开”'),
		)
	);	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'tagid'=>intval($postdata['tagid']),
		'keys'=>$postdata['keys'],
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_scrm_member_settag_key',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_scrm_member_settag_key',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_member_settag_key',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_scrm_member_settag_key',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}